<template>
    <div class="i-table-no-border">
        <Table highlight-row :max-height="560" ref="table" :transfer="true" :columns="type==0?columns1:columns" :data="list"
            :loading="loading" @on-filter-change="handleFilterChange" size="small"
            @on-sort-change="$emit('sortchange', $event)">
            <template slot-scope="{ row }" slot="info">
                <a @click="handleDetailPatient(row)">
                    <Icon type="ios-water" v-if="+row.is_share === 1" />{{
                    row.name
                    }}
                    {{ genderfun(row.gender) }} {{ row.age }}
                </a>
            </template>
            <template slot-scope="{ row }" slot="city_name">
                <span>{{ row.city_name === "0" ? "" : row.city_name }}</span>
            </template>
            <template slot-scope="{ row }" slot="state">
                <Tag v-if="row.status_name === '无效'" color="red">{{
                    row.status_name
                    }}</Tag>
                <Tag v-else-if="row.status_name === '跟进中'" color="volcano">{{
                    row.status_name
                    }}</Tag>
                <Tag v-else-if="row.status_name === '有意向'" color="blue">{{
                    row.status_name
                    }}</Tag>    
                <Tag v-else-if="row.status_name === '预约'" color="cyan">{{
                    row.status_name
                    }}</Tag>
                <Tag v-else-if="row.status_name === '到诊'" color="green">{{
                    row.status_name
                    }}</Tag>
                <Tag v-else-if="row.status_name === '未开口'" color="purple">{{
                    row.status_name
                    }}</Tag>
                <Tag v-else-if="row.status_name === '已到老患者'" color="orange">{{
                    row.status_name
                    }}</Tag>
                <Tag v-else-if="row.status_name" color="orange">{{ row.status_name }}</Tag>
            </template>
            <template slot-scope="{ row }" slot="label">
                <Poptip
                    word-wrap
                    trigger="hover"
                    placement="right"
                    width="400"
                >
                    <template v-for="(item, index) in row.patient_tags_name_arr">
                        <Tag v-show="index<3" :color="item.type==1?'warning':'primary'" :key="index">{{ item.tags_name.replace(/[\r\n]/g, "") }}</Tag>
                    </template>
                    <div slot="content">
                        <template v-for="(item, index) in row.patient_tags_name_arr">
                            <Tag :color="item.type==1?'warning':'primary'" :key="index">{{ item.tags_name.replace(/[\r\n]/g, "") }}</Tag>
                        </template>
                    </div>
                </Poptip>
            </template>
            <template slot-scope="{ row }" slot="labels">
                <Poptip
                    word-wrap
                    trigger="hover"
                    placement="right"
                    maxWidth="400"
                >
                    <Tag
                        color="default"
                        v-for="(item, index) in row.body_parts_name_arr"
                        :key="index"
                        v-if="index < 3"
                        >{{ item.replace(/[\r\n]/g, "") }}</Tag
                    >
                    <div slot="content">
                        <Tag
                            color="default"
                            v-for="(item, index) in row.body_parts_name_arr"
                            :key="index"
                            >{{ item.replace(/[\r\n]/g, "") }}</Tag
                        >
                    </div>
                </Poptip>
            </template>
            <template slot-scope="{ row, index }" slot="action">
                <a v-hasC="{
                        name: 'patient-index-update6',
                        flag: 'v1/patient/index/update',
                    }" @click="handleOpenDrawer(row)">编辑</a>
                <Divider v-hasCC="[
                        {
                            name: 'patient-index-update6',
                            flag: 'v1/patient/index/update',
                        },
                        {
                            name: 'patient-callback-create4',
                            flag: 'v1/patient/callback/create',
                        },
                    ]" type="vertical" />
                <a v-hasC="{
                        name: 'patient-callback-create4',
                        flag: 'v1/patient/callback/create',
                    }" @click="handleReturnVisit(row.id)">
                    回访
                </a>
                <Divider type="vertical" v-if="+row.repeat_type > 0" v-hasC="{
                        name: 'v1-patient-index-repeat-list2',
                        flag: 'v1/patient/index/repeat-list',
                    }" />
                <a @click="copypatient(row)" v-hasC="{
                        name: 'v1-patient-index-repeat-list2',
                        flag: 'v1/patient/index/repeat-list',
                    }" v-if="+row.repeat_type > 0">重复</a>
            </template>
        </Table>
        <patient-detail ref="patientDetail" :btnShow="btnShow" @cancelDrawer="patientfun" @askData="askDatas" />
        <edit-patient ref="editPatient" @patientfun="patientfun" />
        <return-visit ref="returnVisit" @patientfuns="patientfun" />
        <copymodal ref="copymodal" />
    </div>
</template>
<script>
    import patientDetail from '@/components/patientDetail';
    import editPatient from '@/components/editPatient';
    import returnVisit from '@/components/returnVisit';
    import copymodal from '@/pages/patient/copymodal';
    export default {
        props: ['loading', 'list', 'type'],
        components: { patientDetail, editPatient, returnVisit, copymodal },
        data () {
            return {
                modal: false,
                current: 1,
                size: 15,
                showCreate: false,
                creating: true,
                formData: {
                    personnel: [],
                    Occupation: '',
                    Effect: '',
                    risk: '',
                    Diagnosis: ''
                },
                btnShow: [
                    {
                        name: 'patient-index-sub3',
                        flag: 'v1/patient/index/sub'
                    },
                    {
                        name: 'patient-index-edit-remark2',
                        flag: 'v1/patient/index/edit-remark'
                    },
                    {
                        name: 'patient-index-update3',
                        flag: 'v1/patient/index/update'
                    },
                    {
                        name: 'patient-callback-callback-common-list6',
                        flag: 'v1/patient/callback/callback-common-list'
                    },
                    {
                        name: 'patient-index-edit-tags8',
                        flag: 'v1/patient/index/edit-tags'
                    },
                    {
                        name: 'patient-index-edit-tags7',
                        flag: 'v1/patient/index/edit-tags'
                    }
                ],
                filterType: undefined,
                columnss: [],
                columns: [
                    {
                        title: '基本信息',
                        key: 'basic',
                        slot: 'info',
                        width: 150,
                        fixed: 'left'
                    },
                    { title: '患者状态', key: 'state', slot: 'state', width: 120 },
                    { title: '归属人', key: 'belong_member_name', width: 100 },
                    // {
                    //   title: "未访天数",
                    //   sortable: "custom",
                    //   key: "diff_day",
                    //   width: 118,
                    // },
                    {
                        title: '未访天数',
                        sortable: 'custom',
                        key: 'diff_day',
                        filters: [
                            {
                                label: '0-10天未访',
                                value: '1'
                            },
                            {
                                label: '11-20天未访',
                                value: '2'
                            },
                            {
                                label: '21-30天未访',
                                value: '3'
                            },
                            {
                                label: '31-60天未访',
                                value: '4'
                            },
                            {
                                label: '60天以上未访',
                                value: '5'
                            }
                        ],
                        filterMultiple: false,
                        filterRemote (value) {
                            this.filterType = value[0];
                        },
                        width: 118
                    },
                    { title: '部位', slot: 'labels', width: 200 },
                    {
                        title: this.type==1?'回访时间':'待回访时间',
                        key: this.type==1?'last_callback_time_str':'next_callback_time_str',
                        sortable: 'custom',
                        width: 135
                    },
                    { title: '上次回访人', key: 'last_callback_user', width: 108 },
                    {
                        title: '上次回访状态',
                        key: 'last_callback_state_name',
                        width: 118
                    },
                    {
                        title: '回访次数',
                        key: 'callback_count',
                        sortable: 'custom',
                        width: 108
                    },
                    { title: '患者标签', key: 'label', slot: 'label', width: 260 },
                    {
                        title: '城市',
                        key: 'city_name',
                        slot: 'city_name',
                        width: 190
                    },
                    { title: '病种', key: 'patient_diseases_name', width: 200 },
                    {
                        title: '来源',
                        width: 220,
                        key: 'source_name',
                        ellipsis: true,
                        tooltip: true
                    },
                    {
                        title: '备注/回访',
                        minWidth: 250,
                        key: 'remark',
                        ellipsis: true,
                        tooltip: true
                    },
                    { title: '操作', width: 180, slot: 'action', fixed: 'right' }
                ],
                columns1: [
                    {
                        title: '基本信息',
                        key: 'basic',
                        slot: 'info',
                        width: 150,
                        fixed: 'left'
                    },
                    { title: '患者状态', key: 'state', slot: 'state', width: 120 },
                    { title: '归属人', key: 'belong_member_name', width: 100 },
                    // {
                    //   title: "未访天数",
                    //   sortable: "custom",
                    //   key: "diff_day",
                    //   width: 118,
                    // },
                    {
                        title: '未访天数',
                        sortable: 'custom',
                        key: 'diff_day',
                        filters: [
                            {
                                label: '0-10天未访',
                                value: '1'
                            },
                            {
                                label: '11-20天未访',
                                value: '2'
                            },
                            {
                                label: '21-30天未访',
                                value: '3'
                            },
                            {
                                label: '31-60天未访',
                                value: '4'
                            },
                            {
                                label: '60天以上未访',
                                value: '5'
                            }
                        ],
                        filterMultiple: false,
                        filterRemote (value) {
                            this.filterType = value[0];
                        },
                        width: 118
                    },
                    { title: '部位', slot: 'labels', width: 200 },
                    {
                        title: this.type==1?'回访时间':'待回访时间',
                        key: this.type==1?'last_callback_time_str':'next_callback_time_str',
                        sortable: 'custom',
                        width: 135
                    },
                    { title: '上次回访人', key: 'last_callback_user', width: 108 },
                    {
                        title: '上次回访状态',
                        key: 'last_callback_state_name',
                        width: 118
                    },
                    {
                        title: '上次有效回访时间',
                        key: 'last_valid_callback_time_str',
                        width: 140
                    },
                    {
                        title: '回访次数',
                        key: 'callback_count',
                        sortable: 'custom',
                        width: 108
                    },
                    { title: '患者标签', key: 'label', slot: 'label', width: 260 },
                    {
                        title: '城市',
                        key: 'city_name',
                        slot: 'city_name',
                        width: 190
                    },
                    { title: '病种', key: 'patient_diseases_name', width: 200 },
                    {
                        title: '来源',
                        width: 220,
                        key: 'source_name',
                        ellipsis: true,
                        tooltip: true
                    },
                    {
                        title: '备注/回访',
                        minWidth: 250,
                        key: 'remark',
                        ellipsis: true,
                        tooltip: true
                    },
                    { title: '操作', width: 180, slot: 'action', fixed: 'right' }
                ]
            };
        },
        computed: {},
        mounted () {
            if (+this.type === 5) {
                this.columns.splice(4, 0, { title: '剩余天数', width: 110, key: 'daysRemaining' })
            }
        },
        created () { },
        methods: {
            copypatient (row) {
                this.$refs.copymodal.initial(row.id);
            },
            askData () {
                this.columnss = [];               
                setTimeout(() => {
                    this.columnss = JSON.parse(JSON.stringify(this.columns));
                }, 100);
                this.columns[3].filteredValue = ['4'];
            },
            genderfun (type) {
                let gender = '';
                if (+type === 0) gender = '未知';
                else if (+type === 1) gender = '男';
                else if (+type === 2) gender = '女';
                return gender;
            },
            askDatas () {
                this.$store.commit('admin/account/changeCloseRefresh', false);
                this.$emit('askDatas')
            },
            patientfun () {
                console.log(11)
                this.$store.commit('admin/account/changeCloseRefresh', false);
                this.$emit('patientfun');
            },
            handleOpenDrawer (row) {
                this.$refs.editPatient.handleEdit(row);
            },
            handleDetailPatient (row) {
                this.$refs.patientDetail.handleOpen(row);
            },
            handleReturnVisit (id) {
                this.$refs.returnVisit.handleReturnVisit(id);
            },
            handleFilterChange (value) {
                let params = {
                    not_callback_start: '',
                    not_callback_end: ''
                };
                if (+value._filterChecked[0] === 1) {
                    params.not_callback_start = this.$Date()
                        .subtract(10, 'day')
                        .format('YYYY-MM-DD');

                    params.not_callback_end = this.$Date()
                        .format('YYYY-MM-DD');
                } else if (+value._filterChecked[0] === 2) {
                    params.not_callback_start = this.$Date()
                        .subtract(20, 'day')
                        .format('YYYY-MM-DD');

                    params.not_callback_end = this.$Date().subtract(11, 'day').format('YYYY-MM-DD');
                } else if (+value._filterChecked[0] === 3) {
                    params.not_callback_start = this.$Date()
                        .subtract(30, 'day')
                        .format('YYYY-MM-DD');

                    params.not_callback_end = this.$Date()
                        .subtract(21, 'day')
                        .format('YYYY-MM-DD');
                } else if (+value._filterChecked[0] === 4) {
                    params.not_callback_start = this.$Date()
                        .subtract(60, 'day')
                        .format('YYYY-MM-DD');

                    params.not_callback_end = this.$Date()
                        .subtract(31, 'day')
                        .format('YYYY-MM-DD');
                } else if (+value._filterChecked[0] === 5) {
                    params.not_callback_start = '';
                    params.not_callback_end = this.$Date()
                        .subtract(61, 'day')
                        .format('YYYY-MM-DD');
                } else {
                    params.not_callback_start = '';
                    params.not_callback_end = '';
                }
                this.$emit('not_callback', params);
            }
        },
        watch: {
            $route () {
                this.$refs.returnVisit.handleDrawerCancel('formValidate');
            }
        }
    };
</script>
